/******************************************************************************
 * @file       help.cpp
 * @brief      帮助
 *****************************************************************************/
#include "help.h"

#include <QQmlContext>
#include <QJsonValue>

// 静态成员初始化
Help *Help::_help = NULL;

/**
 * @brief 构造函数
 */
Help::Help()
{
    this->setResizeMode(QQuickWidget::SizeRootObjectToView);
    this->setFixedSize(511, 399);
    this->setWindowModality(Qt::ApplicationModal); // 设为模态
    this->setWindowTitle("帮助");

    // 设置属性

    // 暴露自身对象
    this->getContext()->setContextProperty("help", this);

    // 加载qml
    this->setResizeMode(QQuickWidget::SizeRootObjectToView);
    this->setSource(QUrl("qrc:/help.qml"));
}

/**
 * @brief 析构函数
 */
Help::~Help() {}

/**
 * @brief 设置窗口
 * @param text 帮助信息
 */
void Help::helpBox(QWidget *parent, QString text)
{
    if(!_help){
        _help = new Help();
    }

    _help->setHelpMessage(text);
    _help->setParent(parent, Qt::Window | Qt::WindowCloseButtonHint);
    _help->show();
}

/**
 * @brief getContext 获取qmlcontext
 * @return qmlContext
 */
QQmlContext* Help::getContext() const
{
    return this->rootContext();
}

/**
 * @brief getRootObject 获取rootObject
 * @return rootObject
 */
QObject* Help::getRootObject() const
{
    qDebug() << "this->rootObject()" << this->rootObject();
    return (QObject*)this->rootObject();
}

/**
 * @brief 设置帮助信息
 * @param help 帮助信息
 */
void Help::setHelpMessage(QString help)
{
    if(_helpMsg != help){
        _helpMsg = help;
        emit Help::helpMessageChanged(_helpMsg);
    }
}

/**
 * @brief 获取帮助信息
 * @return 帮助信息
 */
QString Help::getHelpMessage() const
{
    return _helpMsg;
}
